package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUser;
import com.ksd.modules.members.mapper.AdminUsersMapper;
import com.ksd.modules.members.po.AdminUserSavePO;
import com.ksd.modules.members.po.AdminUserSearchPO;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台up主用户 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-15
 */
@Service
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUsersMapper, AdminUser> implements IAdminUserService {

    /**
     * 查询列表
     * @param adminUserSearchPO
     * @return
     */
    public Page<AdminUserVo> findAdminUsers(AdminUserSearchPO adminUserSearchPO){
        Page<AdminUser> page = new Page<>(adminUserSearchPO.getPageNo(),adminUserSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUser::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUser::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getStatus()), AdminUser::getStatus,adminUserSearchPO.getStatus());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserSearchPO.getRole()), AdminUser::getRole,adminUserSearchPO.getRole());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getTeacherFlag()), AdminUser::getTeacherFlag,adminUserSearchPO.getTeacherFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getAuthorFlag()), AdminUser::getAuthorFlag,adminUserSearchPO.getAuthorFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getVipFlag()), AdminUser::getVipFlag,adminUserSearchPO.getVipFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getForbidden()), AdminUser::getForbidden,adminUserSearchPO.getForbidden());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getMale()), AdminUser::getMale,adminUserSearchPO.getMale());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserSearchPO.getUuid()), AdminUser::getUuid,adminUserSearchPO.getUuid());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(AdminUser::getNickName, adminUserSearchPO.getKeyword())
                    .or().like(AdminUser::getTelephone, adminUserSearchPO.getKeyword())
                    .or().like(AdminUser::getRealname, adminUserSearchPO.getKeyword())
            );
        }

        lambdaQueryWrapper.orderByDesc(AdminUser::getCreateTime);
        Page<AdminUser> adminUserPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVo> adminUserVoPage = tranferPageBo(adminUserPage, AdminUserVo.class);
        return adminUserVoPage;
    }

    /**
     * 回收站
     * @param adminUserSearchPO
     * @return
     */
    public Page<AdminUserVo> findAdminUsersRecyle(AdminUserSearchPO adminUserSearchPO){
        Page<AdminUser> page = new Page<>(adminUserSearchPO.getPageNo(),adminUserSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUser::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUser::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getStatus()), AdminUser::getStatus,adminUserSearchPO.getStatus());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserSearchPO.getRole()), AdminUser::getRole,adminUserSearchPO.getRole());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getTeacherFlag()), AdminUser::getTeacherFlag,adminUserSearchPO.getTeacherFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getAuthorFlag()), AdminUser::getAuthorFlag,adminUserSearchPO.getAuthorFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getVipFlag()), AdminUser::getVipFlag,adminUserSearchPO.getVipFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getForbidden()), AdminUser::getForbidden,adminUserSearchPO.getForbidden());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserSearchPO.getMale()), AdminUser::getMale,adminUserSearchPO.getMale());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserSearchPO.getUuid()), AdminUser::getUuid,adminUserSearchPO.getUuid());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(AdminUser::getNickName, adminUserSearchPO.getKeyword())
                    .or().like(AdminUser::getTelephone, adminUserSearchPO.getKeyword())
                    .or().like(AdminUser::getRealname, adminUserSearchPO.getKeyword())
            );
        }

        lambdaQueryWrapper.orderByDesc(AdminUser::getCreateTime);
        Page<AdminUser> adminUserPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVo> adminUserVoPage = tranferPageBo(adminUserPage, AdminUserVo.class);
        return adminUserVoPage;
    }

    /**
     * 保存和更新
     * @param adminUserSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminUserVo saveUpdateAdminUser(AdminUserSavePO adminUserSavePO){
        AdminUser adminUser = tranferBo(adminUserSavePO, AdminUser.class);
        adminUser.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUser.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUser);
        AdminUserVo adminUserVo = tranferBo(adminUser, AdminUserVo.class);
        return adminUserVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    @Override
    public AdminUserVo getAdminUserById(Long id){
        AdminUser adminUser = this.getById(id);
        AdminUserVo adminUserVo = tranferBo(adminUser, AdminUserVo.class);
        return adminUserVo;
    }

    /**
     * 根据ID获取明细
     * @param uuid
     * @return
     */
    @Override
    public AdminUserVo getAdminUserByUUId(String uuid){
        LambdaQueryWrapper<AdminUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUser::getUuid,uuid);
        AdminUser adminUser = this.getOne(lambdaQueryWrapper);
        AdminUserVo adminUserVo = tranferBo(adminUser, AdminUserVo.class);
        return adminUserVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminUserById(Long id){
        boolean b =  this.updateStatus(id, AdminUser.Columns.IS_DELETED,1);
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminUser(Long id){
        boolean b =  this.updateStatus(id, AdminUser.Columns.IS_DELETED,0);
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminUser(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminUser(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUser(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }


}
